package com.huishu.order.controller;


import com.alibaba.fastjson.JSON;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.pojo.vo.UserOrderInfoVO;
import com.huishu.order.pojo.dto.CreateOrderDTO;
import com.huishu.order.pojo.dto.ExpressDeliveryDTO;
import com.huishu.order.pojo.dto.PageListDTO;
import com.huishu.order.pojo.dto.UpdateAddressDTO;
import com.huishu.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 订单表前端控制器
 *
 * @author generator@ljj
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/express-delivery-notify")
    public Result expressDeliveryNotify(@RequestBody @Valid ExpressDeliveryDTO expressDeliveryDTO) {
        log.info("快递信息回传");
        return orderService.expressDeliveryNotify(expressDeliveryDTO);
    }

    @PostMapping("/createOrder")
    public Result createOrder(@RequestBody CreateOrderDTO createOrderDTO, HttpServletRequest request) {
        log.info("创建订单：{}", JSON.toJSONString(createOrderDTO));
        return orderService.createOrder(createOrderDTO, request);
    }

    @PostMapping("/notify")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) {
        orderService.payNotify(request, response);
    }

    @GetMapping("/order-detailed/{id}")
    public Result orderDetailed(@PathVariable Long id) {
        return orderService.orderDetailed(id);
    }

    @GetMapping("/findByType/{type}")
    public Result findByType(@PathVariable Integer type) {
        return orderService.findByType(type);
    }


    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestBody UpdateAddressDTO updateAddressDTO) {
        log.info("修改收货地址：{}", JSON.toJSONString(updateAddressDTO));
        return orderService.updateAddress(updateAddressDTO);
    }

    @GetMapping("/confirm/{id}")
    public Result confirm(@PathVariable Long id) {
        return orderService.confirm(id);
    }

    @GetMapping("/continue-pay/{orderSn}")
    public Result continuePay(@PathVariable String orderSn) {
        return orderService.continuePay(orderSn);
    }

    @GetMapping("/cancel/{orderSn}")
    public Result cancelOrder(@PathVariable String orderSn) {
        orderService.cancelOrderRollbackStock(orderSn);
        return Result.success();
    }
    /*后台管理*/

    @PostMapping("/pageList")
    public Result pageList(@RequestBody PageListDTO pageListDTO) {
        return orderService.pageList(pageListDTO);
    }


    @PostMapping("/export")
    public void export(@RequestBody PageListDTO pageListDTO, HttpServletResponse response) {
        orderService.export(pageListDTO, response);
    }

    @PostMapping("/getOrderInfoByUserIds")
    public Result<List<UserOrderInfoVO> >  getOrderInfoByUserIds(@RequestBody List<Long> ids) {
        return orderService.getOrderInfoByUserIds(ids);
    }

    @GetMapping("/refund/{id}")
    public Result refund(@PathVariable Long id){
        return orderService.refund(id);
    }

    @GetMapping("/application-chargeback/{id}")
    public Result applicationExchange(@PathVariable Long id){
        return orderService.applicationExchange(id);
    }


    @GetMapping("/application-refund/{id}")
    public Result applicationRefund(@PathVariable Long id){
        return orderService.applicationRefund(id);
    }

    @GetMapping("/application-refund-goods/{id}")
    public Result applicationRefundGoods(@PathVariable Long id){
        return orderService.applicationRefundGoods(id);
    }

    @GetMapping("/chargeback/{id}")
    public Result chargeback(@PathVariable Long id){
        return orderService.chargeback(id);
    }

    @GetMapping("/refund-chargeback/{id}")
    public Result refundChargeback(@PathVariable Long id){
        return orderService.refundChargeback(id);
    }

    @GetMapping("/cancellation/{id}")
    public Result cancellation(@PathVariable Long id){
        return orderService.cancellation(id);
    }

    @GetMapping("/background-details/{id}")
    public Result backgroundDetails(@PathVariable Long id){
        return orderService.backgroundDetails(id);
    }

    @GetMapping("/refuse-refund/{id}")
    public Result refuseRefund(@PathVariable Long id){
        return orderService.refuseRefund(id);
    }

    @GetMapping("/refuse-chargeback/{id}")
    public Result refuseChargeback(@PathVariable Long id){
        return orderService.refuseChargeback(id);
    }

    @GetMapping("/refuse-chargeback-refund/{id}")
    public Result refuseChargebackRefund(@PathVariable Long id){
        return orderService.refuseChargebackRefund(id);
    }
}
